package cn.edu.lit.jsjxy.CourseExample.chapter5;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import cn.edu.lit.jsjxy.CourseExample.R;

public class ServiceTestActivity extends Activity implements View.OnClickListener
{

    String TAG = this.getClass().getSimpleName();

    private MyService.DownloadBinder downloadBinder;

    Button btnStartDownload, btnGetProcess;
    String btnTitle;

    private ServiceConnection connection = new ServiceConnection()
    {

        @Override
        public void onServiceDisconnected(ComponentName name)
        {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service)
        {
            downloadBinder = (MyService.DownloadBinder) service;
            btnStartDownload.setEnabled(true);
            btnGetProcess.setEnabled(true);

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_test);

        Button startService = (Button) findViewById(R.id.start_service);
        Button stopService = (Button) findViewById(R.id.stop_service);
        Button bindService = (Button) findViewById(R.id.bind_service);
        Button unbindService = (Button) findViewById(R.id.unbind_service);
        Button startIntentService = (Button) findViewById(R.id.start_intent_service);

        btnStartDownload = findViewById(R.id.btnStartDownload);
        btnGetProcess = findViewById(R.id.btnGetProgress);

        startService.setOnClickListener(this);
        stopService.setOnClickListener(this);
        bindService.setOnClickListener(this);
        unbindService.setOnClickListener(this);
        startIntentService.setOnClickListener(this);

        btnStartDownload.setOnClickListener(this);
        btnGetProcess.setOnClickListener(this);

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        btnStartDownload.setEnabled(false);
        btnGetProcess.setEnabled(false);
        btnTitle = btnGetProcess.getText().toString();
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.start_service:
                Intent startIntent = new Intent(this, MyService.class);
                startService(startIntent); // 启动服务
                break;
            case R.id.stop_service:
                Intent stopIntent = new Intent(this, MyService.class);
                stopService(stopIntent); // 停止服务
                break;
            case R.id.bind_service:
                Intent bindIntent = new Intent(this, MyService.class);
                bindService(bindIntent, connection, BIND_AUTO_CREATE); // 绑定服务

                break;
            case R.id.unbind_service:
                unbindService(connection); // 解绑服务
                btnStartDownload.setEnabled(false);
                btnGetProcess.setEnabled(false);
                break;
            case R.id.start_intent_service:
                // 打印主线程的id
                Log.d(TAG, "Thread id is " + Thread.currentThread().getId());
                Intent intentService = new Intent(this, MyIntentService.class);
                startService(intentService);
                break;

            case R.id.btnStartDownload:
//                Intent startIntent = new Intent(this, MyService.class);
//                startService(startIntent); // 启动服务
                downloadBinder.startDownload();
                break;

            case R.id.btnGetProgress:
//                Intent startIntent = new Intent(this, MyService.class);
//                startService(startIntent); // 启动服务
                int k = downloadBinder.getRandomInt(1000);
                btnGetProcess.setText("" + k);
                break;
            default:
                break;
        }
    }
}
